<!--pages/hot/hot.vue-->
<script setup>
import { ref } from 'vue'
import { getHotRecommendAPI } from '../../services/hot'
import { onLoad } from '@dcloudio/uni-app'
// 热门推荐页 标题和url
const urlMap = [
  { type: '1', title: '特惠推荐', url: '/hot/preference' },
  { type: '2', title: '爆款推荐', url: '/hot/inVogue' },
  { type: '3', title: '一站买全', url: '/hot/oneStop' },
  { type: '4', title: '新鲜好物', url: '/hot/new' },
]
// uniapp里
// 如果一个.vue文件是页面文件，那么defineProps接收的是上个页面传入的页面参数(页面传参)
// 如果一个.vue文件是组件文件，那么defineProps接收的是这个组件的数据(父子传参)
// 获取页面传参
const query = defineProps({
  type: String
})
// 设置标题
const currUrlMap = urlMap.find(item => item.type === query.type)
uni.setNavigationBarTitle({ title: currUrlMap.title })


// 定义变量
// 推荐的封面图
const bannerPicture = ref('')
// 推荐选项
const subTypes = ref([])
// 高亮
const activeIndex = ref(0)
// 获取列表数据方法
const getHotRecommendData = async () => {
  const { result } = await getHotRecommendAPI(currUrlMap.url, {
    pageSize: 10,
    page: import.meta.env.DEV ? 30 : 1
  })
  console.log(result)
  bannerPicture.value = result.bannerPicture
  subTypes.value = result.subTypes
}
// 初始化调用
onLoad(() => {
  getHotRecommendData()
})
// 滚动到底
const onScrolltolower = async () => {
  // 1.获取当前项目
  const curSubTypes = subTypes.value[activeIndex.value]
  console.log(curSubTypes)
  // 2.判断是否结束(page和pages比较)  页码+1
  if (curSubTypes.goodsItems.page < curSubTypes.goodsItems.pages) {
    curSubTypes.goodsItems.page++
  } else {
    curSubTypes.finish = true
    return uni.showToast({ title: '暂无更多数据', icon: 'none'})
  }
  // 3.调用并传参
  const { result } = await getHotRecommendAPI(currUrlMap.url, {
    subType: curSubTypes.id,
    pageSize: curSubTypes.goodsItems.pageSize,
    page: curSubTypes.goodsItems.page
  })
  console.log(result)
  // 4. 获取最新数据
  const newSubTypes = result.subTypes[activeIndex.value]
  console.log(newSubTypes)  // 新数据
  // console.log(subTypes[activeIndex.value])  // 老数据
  curSubTypes.goodsItems.items.push(...newSubTypes.goodsItems.items)
}
</script>

<template>
  <view class="viewport">
    <!-- 推荐封面图 -->
    <view class="cover">
      <image
        :src="bannerPicture"
      />
    </view>
    <!-- 推荐选项 -->
    <view class="tabs">
      <text class="text" v-for="(item, index) in subTypes" :key="item.id"
      @click="activeIndex = index"
      :class="{ active: index === activeIndex }"
      >{{item.title}}</text>
    </view>
    <!-- 推荐列表 -->
    <scroll-view 
    @scrolltolower="onScrolltolower" 
    scroll-y 
    class="scroll-view" 
    v-for="(item, index) in subTypes" 
    :key="item.id" 
    v-show="index === activeIndex">
      <view class="goods">
        <navigator
          hover-class="none"
          class="navigator"
          v-for="goods in item.goodsItems.items"
          :key="index"
          :url="`/pages/goods/goods?id=${goods.id}`"
        >
          <image
            class="thumb"
            :src="goods.picture"
          ></image>
          <view class="name ellipsis">{{goods.name}}</view>
          <view class="price">
            <text class="symbol">¥</text>
            <text class="number">{{goods.price}}</text>
          </view>
        </navigator>
      </view>
      <view class="loading-text">{{item.finish ? '暂无更多数据' : '正在加载...'}}</view>
    </scroll-view>
  </view>
</template>

<style lang="scss">
page {
  height: 100%;
  background-color: #f4f4f4;
}
.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 180rpx 0 0;
  position: relative;
}
.cover {
  width: 750rpx;
  height: 225rpx;
  border-radius: 0 0 40rpx 40rpx;
  overflow: hidden;
  position: absolute;
  left: 0;
  top: 0;
}
.scroll-view {
  flex: 1;
}
.tabs {
  display: flex;
  justify-content: space-evenly;
  height: 100rpx;
  line-height: 90rpx;
  margin: 0 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  box-shadow: 0 4rpx 5rpx rgba(200, 200, 200, 0.3);
  color: #333;
  background-color: #fff;
  position: relative;
  z-index: 9;
  .text {
    margin: 0 20rpx;
    position: relative;
  }
  .active {
    &::after {
      content: '';
      width: 40rpx;
      height: 4rpx;
      transform: translate(-50%);
      background-color: #27ba9b;
      position: absolute;
      left: 50%;
      bottom: 24rpx;
    }
  }
}
.goods {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  padding: 0 20rpx 20rpx;
  .navigator {
    width: 345rpx;
    padding: 20rpx;
    margin-top: 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }
  .thumb {
    width: 305rpx;
    height: 305rpx;
  }
  .name {
    height: 88rpx;
    font-size: 26rpx;
  }
  .price {
    line-height: 1;
    color: #cf4444;
    font-size: 30rpx;
  }
  .symbol {
    font-size: 70%;
  }
  .decimal {
    font-size: 70%;
  }
}

.loading-text {
  text-align: center;
  font-size: 28rpx;
  color: #666;
  padding: 20rpx 0 50rpx;
}
</style>